using System;
using System.Collections.Generic;
using System.Linq;
using Ninject;
using sdm.exam.domain.Abstract;

namespace sdm.exam.domain.DataContext
{
    public class TeacherDomain
    {
        private readonly ISdmExamRepository _repository;
        public TeacherDomain()
        {
            _repository = Ninjector.Kernel.Get<ISdmExamRepository>();
            Ninjector.Kernel.Bind<ISdmExamRepository>().ToConstant(new DataContext(ConnectionStrings.StandardConnection));
        }
        /// <summary>
        /// Checks if the teacher has the course. 
        /// </summary>
        /// <param name="course">The course to check whether or not the teacher has it.</param>
        /// <param name="teachersCourses">The collection of what courses the teacher has.</param>
        /// <returns>A boolean indicating whether or not, the teacher has the specific course.</returns>
        public bool TeacherHasCourse(Entities.Course course, ICollection<Entities.Course> teachersCourses)
        {
            var selectedCouse = course.Name;
            var courseName = (teachersCourses.Where(item => item.Name == selectedCouse).Select(item => course.Name));
            return true;
        }

        /// <summary>
        /// Gets a list of courses the teacher has. 
        /// </summary>
        /// <returns>Get a list of teachers connected to course.</returns>
        public List<Entities.TeacherCourse> GetListOfAllCourseConnectedToTeacher()
        {
            throw new NotImplementedException();
        }


        public bool AddTeacherToSubjectOfSemesterClass(Entities.Teacher teacher, Entities.Course course, Entities.SemesterClass semesterClass)
        {
            Entities.Course currentCourse = null;
            var studyplan = _repository.Studyplans.FirstOrDefault(m => m.SemesterClass.Id == semesterClass.Id);
            if (!TeacherHasCourse(course, teacher.Courses))
                return false;

            if (studyplan != null)
            {
                currentCourse = studyplan.Courses.FirstOrDefault(m => m.Id == course.Id);
            }
            else
                return false;

            if (currentCourse != null)
            {
                var teacherCourse = new Entities.TeacherCourse() { Course = currentCourse, Teacher = teacher };
                _repository.TeacherCourses.Add(teacherCourse);
                _repository.SaveChanges();
            }
            else
                return false;
            return true;
        }

        public bool AddTeacher(Entities.Teacher teacher)
        {
            try
            {
                _repository.Teachers.Add(teacher);
                _repository.SaveChanges();
                return true;
            }
            catch(Exception e)
            {
                return false;
            }
        }
    }
}
